@echo off
setlocal enabledelayedexpansion
set name=%~nx0
set version=1.3
set true=1
set false=0
set return=%false%
set canrun=%true%
REM ---------------------------
REM ---USER DEFINED SETTINGS---
set destination=%CD%
set execloc=%destination:~0,-3%bin
set objectsloc=%destination%\objects
REM ---------------------------

for /f "tokens=* delims=^=" %%x in (makefile) do (
	set temp=%%x
	set temp=!temp:EXECUTABLE=!
	if not %%x==!temp! (
		set execname=!temp:~1!
		GOTO startloop
	)
)

if "%execname%"=="" (
	echo Executable name can not be determined.
	set canrun=%false%
)

:startloop
echo %name% v%version% (%CD%)
echo -
set /p target="target?: "

if "%target%" == "all" (
	echo MOVING FILES
	CALL :move *.o %objectsloc% %destination%
	echo.
	echo BUILDING...
	CALL :make
	echo.
	echo MOVING FILES
	CALL :move *.o %destination% %objectsloc%
	CALL :move *.exe %destination% %execloc%
) else ( if "%target%" == "clean" (
	echo DELETING FILES
	if exist %execloc%\*.exe (
		del %execloc%\*.exe>NUL
		echo *.exe succesfully deleted
	)
	
	if exist %objectsloc%\*.o (
		del %objectsloc%\*.o>NUL
		echo *.o succesfully deleted
	)
) else ( if not x%target:.o=%==x%target% (
	echo BUILDING...
	CALL :make
	echo. 
	echo MOVING FILES
	CALL :move %target% %destination% %objectsloc%
) else ( if "%target%" == "x" (
	EXIT
) else ( if "%target%" == "r" (
	if !canrun! == %true% (
		start /d%execloc% %execname%.exe
	) else (
		echo r command is disabled.
		GOTO startloop
	)
) else (
	echo.
	echo all, clean, x, *.o, r
	GOTO startloop
)))))

echo.
GOTO startloop

REM -----subroutines------
:make             
mingw32-make %target%
GOTO :EOF

REM %1=file name %2=folder %3=destination
:move
if exist %2\%1 (
	move %2\%1 %3>NUL
	echo %1 successfully moved
	set return=%true%
) else (
	echo %1 NOT found
	set return=%false%
)
GOTO :EOF
